package top.truehaku.generator.common.enums;


import com.google.common.collect.Sets;
import org.springframework.util.CollectionUtils;
import top.truehaku.generator.common.util.StringUtils;

import java.math.BigDecimal;
import java.sql.Time;

import java.util.Collections;
import java.util.Date;
import java.util.Set;

public enum JavaType {
    STRING(String.class, false, Sets.newHashSet("tinytext", "text", "mediumtext", "longtext", "char", "varchar", "nvarchar", "varchar2")),
    BOOLEAN(Boolean.class, true, Sets.newHashSet("bit")),
    INTEGER(Integer.class, false, Sets.newHashSet("tinyint", "smallint", "mediumint", "int")),
    LONG(Long.class, false,Sets.newHashSet(Collections.singleton("bigint"))),
    DOUBLE(Double.class, false,Sets.newHashSet("float", "double")),
    DATETIME(Date.class, true,Sets.newHashSet("datetime", "timestamp")),
    DATE(Date.class, false,Sets.newHashSet("date")),
    DECIMAL(BigDecimal.class, true,Sets.newHashSet("decimal", "numeric")),
    TIME(Time.class, true,Sets.newHashSet("time")),
    BLOB(byte[].class, false,Sets.newHashSet("blob")),
    UNKNOWN(null, false,null)
    ;

    private final Class<?> clazz;
    private final boolean referenceRequired;
    private final Set<String> columnTypes;

    JavaType(Class<?> clazz, boolean referenceRequired,Set<String> columnTypes) {
        this.clazz = clazz;
        this.referenceRequired = referenceRequired;
        this.columnTypes = columnTypes;
    }

    public String getSimpleName(){
        return this.clazz.getSimpleName();
    }

    public String getReference(){
        return this.referenceRequired?this.clazz.getName():null;
    }

    //fixme
    public static JavaType getJavaType(String columnType){
        if(StringUtils.isEmpty(columnType)){
            throw new IllegalArgumentException("字段类型不能为空");
        }
        for (JavaType javaType:JavaType.values()) {
            if (!CollectionUtils.isEmpty(javaType.columnTypes)&&javaType.columnTypes.contains(columnType)){
                return javaType;
            }
        }
        return null;
    };

    public static void main(String[] args) {
        JavaType varchar = JavaType.getJavaType("varchar");
        System.out.println();
    }

}
